<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use think\Controller;
use think\Config;
use think\Loader;
use think\Db;

class AdminController extends BaseController
{
    //主页
    public function index(){
        if (request()->isAjax()){
            $page=\input("page");
            $keyword=\input("param.keyword");
            $limit=\input("param.limit",100);
            $where=[];
            $where="a.status in (1,2)";
            if ($keyword){
                $where="a.names like '%{$keyword}%'";
            }
            $f[]="a.*";
            $f[]="ar.role_name";

            $sum=($page-1)*$limit;
            $data=Db::name("admin")
                ->alias('a')
                ->join("auth_role ar"," a.role_id=ar.role_id","left")
                ->where($where)
                ->field($f)
                ->limit("$sum,$limit")
                ->select();
            $num=Db::name("admin")
                ->alias('a')
                ->join("auth_role ar"," a.role_id=ar.role_id","left")
                ->where($where)
                ->field($f)
                ->select();
            $result['code'] = 0;
            $result['msg'] = "";
            $result['count'] = count($num);
            $result['data'] = $data;
            return json($result);
        }
        return $this->fetch("index");
    }

    /*
     * 添加管理员
     */
    public function add(){
        if (request()->isPost()){
            $list=request()->post();
            $list["password"]=pswCrypt($list["password"]);
            $list["add_time"]=time();
            $find=Db::name("admin")->where(["names"=>$list["names"]])->find();
            if ($find){
                return json(["code"=>0,"msg"=>"已存在该管理员"]);
            }
            $result=Db::name("admin")->insert($list);
            if ($result){
                return json(["code"=>1,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>0,"msg"=>"添加失败"]);
            }

        }

        $role=Db::name("auth_role")->select();
        $this->assign("role",$role);
        return $this->fetch("add");
    }

    /*
     * 编辑
     */
    public function edit(){
        $id=input("param.id");
        if (request()->isPost()){
            $list=request()->post();
            $result=Db::name("admin")->where(["id"=>$list["id"]])->update($list);
            if ($result){
                return json(["code"=>1,"msg"=>"编辑成功"]);
            }else{
                return json(["code"=>0,"msg"=>"编辑失败"]);
            }
        }
        $find=Db::name("admin")->where(["id"=>$id])->find();
        $this->assign("find",$find);
        $role=Db::name("auth_role")->select();
        $this->assign("role",$role);
        return  $this->fetch("edit");

    }

    /*
     * 删除
     */
    public function del(){
        $id=input("param.id");
        $result=Db::name("admin")->where(["id"=>$id])->delete();
        writelog("删除管理员成功");
        if ($result){
            return json(["code"=>1,"msg"=>"删除成功"]);
        }else{
            return json(["code"=>0,"msg"=>"删除失败"]);
        }
    }
}